package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.vo.MarketOrderReadVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminOrderServiceImpl implements AdminOrderService{
    @Override
    public List<MarketOrder> list(String page, String limit, String sort, String order, String orderStatusArray, String status, String end, String userId, String orderSn) {
        SqlSession session= MybatisUtils.getSession();
        MarketOrderMapper mapper = session.getMapper(MarketOrderMapper.class);
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (!StringUtils.isEmpty(orderStatusArray)){
            criteria.andOrderStatusEqualTo(Short.valueOf(orderStatusArray));
        }
        if (!StringUtils.isEmpty(status)&&StringUtils.isNotEmpty(end)){
            criteria.andAddTimeBetween(LocalDateTime.parse(status),LocalDateTime.parse(end));
        }
        if (!StringUtils.isEmpty(userId)){
            criteria.andIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(orderSn)){
            criteria.andOrderSnEqualTo(orderSn);
        }
        List<MarketOrder> list=mapper.selectByExample(marketOrderExample);
        session.close();
        return list;
    }
        @Override
        public Map<String, Object> detail(String id) {
            Map<String,Object> resultData = new HashMap<String,Object>();
            SqlSession session = MybatisUtils.getSession();

            MarketOrderMapper marketOrderMapper = session.getMapper(MarketOrderMapper.class);
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            marketOrderExample.createCriteria().andIdEqualTo(Integer.valueOf(id));
            MarketOrder marketOrder = marketOrderMapper.selectOneByExample(marketOrderExample);
            session.commit();
            resultData.put("order",marketOrder);

            MarketOrderGoodsMapper marketOrderGoodsMapper = session.getMapper(MarketOrderGoodsMapper.class);
            MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
            marketOrderGoodsExample.createCriteria().andOrderIdEqualTo(Integer.valueOf(id));
            List<MarketOrderGoods> orderGoodsList = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
            session.commit();

            resultData.put("orderGoods",orderGoodsList);

            MarketUserMapper marketUserMapper = session.getMapper(MarketUserMapper.class);
            MarketUserExample marketUserExample = new MarketUserExample();
            marketUserExample.createCriteria().andIdEqualTo(Integer.valueOf(marketOrder.getUserId()));
            MarketUser marketUser = marketUserMapper.selectOneByExample(marketUserExample);
            Map<String,String> user =new HashMap<>();
            user.put("nickname",marketUser.getNickname());
            user.put("avatar",marketUser.getAvatar());
            resultData.put("user",user);

            return resultData;
    }

    @Override
    public Integer refund(MarketOrder marketOrder) {
        SqlSession session=MybatisUtils.getSession();
        MarketOrderMapper marketOrderMapper = session.getMapper(MarketOrderMapper.class);
        Integer i=marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        session.commit();
        session.close();
        return i;
    }
}
